package net.mihome.app.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;

import net.mihome.app.R;
import net.mihome.app.bean.Group;
import net.mihome.app.bean.URLs;


import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhao on 2015/2/7.
 */
public class FriendGroupAdapter extends BaseAdapter {
    private List<Group> list=new ArrayList<>();
    private LayoutInflater listContainer;
    private Context context;
    ImageLoader imageLoader;
    public FriendGroupAdapter(Context context,List<Group> list){
        this.context=context;
        this.list=list;
        listContainer = LayoutInflater.from(context);
        imageLoader=ImageLoader.getInstance();
    }
    public static class Views{
        ImageView avatar;
        TextView name;
        TextView desc;
    }
    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return Long.parseLong(list.get(i).getGroupid());
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        Views views=null;
        if(view==null||view.getTag()==null){
            view = listContainer.inflate(R.layout.row_group, viewGroup, false);
            views=new Views();
            views.avatar= (ImageView) view.findViewById(R.id.avatar);
            views.desc= (TextView) view.findViewById(R.id.desc);
            views.name= (TextView) view.findViewById(R.id.name);
            view.setTag(views);
        }else{
            views= (Views) view.getTag();
        }
        Group group= (Group) getItem(i);
        imageLoader.displayImage(URLs.MIHOME_GROUP_AVATAR + group.getGroupid()+".jpg",views.avatar);
        views.desc.setText(group.getDescription());
        views.name.setText(group.getGroupname());
        return view;
    }
}
